function IsNullOrEmpty($str)
{
    if ($str)
    {
        return $false;
    }
    else
    {
        return $true;
    }
}

# --------------- #
# Load Assemblies #
# --------------- #

if ([Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") -eq $null) { throw "Unable to load 'Microsoft.SharePoint.dll'!"; }

$count = 0;
foreach ($s in (Get-PSSnapIn | where { $_.Name -eq "Microsoft.SharePoint.PowerShell"; })) { $count++; }

if ($count -eq 0)
{
	Add-PSSnapIn "Microsoft.SharePoint.PowerShell"
}

# ---------- #
# Parameters #
# ---------- #

$siteCollections = @();
$count = 1;

Write-Host ""
Write-Host "SharePoint Site Collections on your local machine:"
Write-Host "=================================================="

foreach ($Item in Get-SPSite)
{    
    Write-Host $count "-" $Item.Url
        
    $siteCollections += $Item.Url
    $count++;
}

Write-Host ""

$validSiteCollection = $FALSE;
while (-not $validSiteCollection)
{
    $index = Read-Host "Enter a number to identify the Site Collections where to create the site"
    
    $validSiteCollection = ($index -gt 0) -and ($index -le $siteCollections.Length)
}

[string]$siteUrl = $siteCollections[$index - 1]
if (!$siteUrl.EndsWith("/")) { $siteUrl += "/"; }

# --------------------------- #
# Activate Publishing Feature #
# --------------------------- #
$result = Get-SPSite $siteUrl -ErrorVariable errorMessage -ErrorAction SilentlyContinue
if ($errorMessage){ throw "The site collection at $siteUrl could not be found. Verify that you have typed the URL correctly."; }

""
"Activating Required Site Collection Features at $siteUrl...";
Enable-SPFeature -identity "f6924d36-2fa8-4f0b-b16d-06b7250180fa" -URL $siteUrl -force;
Enable-SPFeature -identity "94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb" -URL $siteUrl -force;
Enable-SPFeature -identity "a1cb5b7f-e5e9-421b-915f-bf519b0760ef" -URL $siteUrl -force;
"Required features activated at $siteUrl!";

# --------------------------- #
# Create Web Site             #
# --------------------------- #
$webSiteUrl = $siteUrl + [string]"PerformancePoint2010Demo";

$result = Get-SPWeb $webSiteUrl -ErrorVariable errorMessage -ErrorAction SilentlyContinue
if (IsNullOrEmpty $errorMessage)
{
    ""
    "Deleting the web site at $webSiteUrl..."
    Remove-SPWeb -Identity $webSiteUrl -Confirm:$false    
    "$webSiteUrl web site deleted!"
}

""
"Creating the web site at $webSiteUrl..."
$result = Get-SPWebTemplate | Where{ $_.Title -eq "Business Intelligence Center" } | ForEach-Object{ New-SPWeb -Url $webSiteUrl -Template $_ -Name "Delivering Information to Decision Makers with a Microsoft BI Solution Demo" -Description "This web site was created during the setup process of the Delivering Information to Decision Makers with a Microsoft BI Solution demo." }
"$webSiteUrl web site created!"
""